﻿/**
* QSmart Andon
*
* 2012-10-11
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.SystemManage
{
    public class RoleRightService : DbHelperBase
    {
        public bool AddRoleRight(string roleId, List<RoleRightEntity> list)
        {
            bool result;
            if (GetRoleRightCountByRoleId(roleId) > 0)
            {
                RemoveRoleRightByRoleId(roleId);
                result = BatchAdd(list);
            }
            else
            {
                result = BatchAdd(list);
            }
            return result;
        }

        public bool RemoveRoleRightByRoleId(string roleId)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_RoleRight Where RoleID=@RoleID ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", roleId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        private int GetRoleRightCountByRoleId(string roleId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_RoleRight WHERE  RoleID=@RoleID ");
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleID", roleId);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        private bool BatchAdd(List<RoleRightEntity> list)
        {
            var result = false;
            var conn = Db.GetConnection() as SqlConnection;
            if (conn == null)
            {
                throw new InvalidOperationException("No Valid Database Connection!");
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            using (var bcp = new SqlBulkCopy(conn))
            {
                var mapRightId =
                    new SqlBulkCopyColumnMapping("RightId", "RightId");
                bcp.ColumnMappings.Add(mapRightId);

                var mapRoleId =
                    new SqlBulkCopyColumnMapping("RoleID", "RoleID");
                bcp.ColumnMappings.Add(mapRoleId);

                var mapMenuId =
                    new SqlBulkCopyColumnMapping("MenuID", "MenuID");
                bcp.ColumnMappings.Add(mapMenuId);

                bcp.DestinationTableName = "UT_RoleRight";

                try
                {
                    bcp.WriteToServer(Collection.ToDataTable(list));
                    result = true;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            return result;
        }

        public List<RoleRightEntity> GetMenuIdForRoleRight(string roleId)
        {
            var list = new List<RoleRightEntity>();
            var strSql = new StringBuilder();
            strSql.Append("SELECT MenuID FROM UT_RoleRight WHERE RoleId=@RoleId");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RoleId", roleId);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new RoleRightEntity();
                            if (reader["MenuID"] != DBNull.Value)
                                model.MenuID = new Guid(reader["MenuID"].ToString());
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
    }
}
